

module.exports = app => {
  const DataTypes = app.Sequelize;

  const Orders = app.model.define('orders', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '订单状态【 0-待支付, 1-已支付, 2-待发货, 3-已发货, 4-退款中, 5-已退款, 6-已完成, 7-已取消】'
    },
    totals: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '订单总价'
    },
    coupon_totals: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '优惠券抵扣金额'
    },
    remark: {
      type: DataTypes.STRING(255),
      comment: '订单备注'
    },
    uid: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '用户id',
      references: {
        model: 'users',
        key: 'id'
      }
    },
    aid: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '订单-地址_id',
      references: {
        model: 'address',
        key: 'id'
      }
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '创建时间'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '更新时间'
    }
  }, {
    tableName: 'orders'
  });

  Orders.associate = function () {
    //一个订单属于一个用户
    app.model.Orders.belongsTo(app.model.Users, {
      foreignKey: 'uid',//定义联结关系中源模型的 key
      targetKey: 'id'
    });

    //一个订单属于一个地址
    app.model.Orders.belongsTo(app.model.Address, {
      foreignKey: 'aid',//定义联结关系中源模型的 key
      targetKey: 'id'
    });
  }

  return Orders;
};
